/**
 * @anchor jiuyao2019@163.com
 * @name jiuyao
 * @version 1.0
 * @date 2024-08-08
 * @brief this is a simple format library,use it to encode and decode data ,"spf" is short for "simple format"
 * @note the simple format program uses the standard C library,suggest to check the C environment support before using it.
 * @example the data encode as follows: name:ZhangSan,age:24,weight:60.500
 */


#ifndef __SPF_H__
#define __SPF_H__


#ifdef __cplusplus
extern "C" {
#endif




	//define the separator of key and value
#define CFG_KV_SEPARATOR	':'

	// define the terminator of a couple of data
#define CFG_TERMINATOR		';'




	/// @brief the structure of spf
	typedef struct spf
	{
		char* payload;
		int len;
		int ptr;
	}spf_t;



	/**
	 *@brief initialize the spf structure,and allocate memory for it
	 *
	 * @param spf the instance of spf structure
	 * @param len	the length of the memory to be allocated
	 * @return int if success return 0,else return -1
	 */
	int spf_encode_init(spf_t* spf, int len);

	/**
	 *@brief deinitialize the spf structure,and release the memory for it
	 *
	 * @param spf the instance of spf structure
	 * @param len if success return 0,else return -1
	 */
	void spf_encode_deinit(spf_t* spf);

	/**
	 *@brief add int value to spf
	 *
	 * @param spf the instance of spf structure
	 * @param key the key of the value
	 * @param value the int value to be added
	 * @return int if success return 0,else return -1
	 */
	int spf_encode_add_int(spf_t* spf, char* key, int value);


	/**
	 *@brief add string value to spf
	 *
	 * @param spf the instance of spf structure
	 * @param key the key of the value
	 * @param value the string value to be added
	 * @return int if success return 0,else return -1
	 */
	int spf_encode_add_string(spf_t* spf, char* key, char* value);

	/**
	 *@brief add float value to spf
	 *
	 * @param spf the instance of spf structure
	 * @param key the key of the value
	 * @param value the float value to be added
	 * @return int if success return 0,else return -1
	 */
	int spf_encode_add_float(spf_t* spf, char* key, float value);



	/**
	 *@brief allocate memory for payload and copy the payload to it,you can release the origal payload after calling this function
	 *
	 * @param spf the instance of spf structure
	 * @param payload the origal payload,you can release the origal payload after calling this function
	 * @param len the length of origal payload,if origal payload has some useless ,
	 * set len and spf will not deal with the data pass the len.But you should promise that the payload real length is not less than len parameter
	 * @return int if success return 0,else return -1
	 */
	int spf_decode_init(spf_t* spf, char* payload, int len);


	/**
	 *@brief deinitialize the spf structure,and release the memory for it
	 *
	 * @param spf the instance of spf structure
	 * @param len if success return 0,else return -1
	 */
#define spf_decode_deinit(spf) spf_encode_deinit(spf)

	 /**
		*@brief get int value from spf
		*
	* @param spf the instance of spf structure
	* @param key the key of the int value you want to get
	* @param value the int value you want to get
	* @return int if success return 0,else return -1
	*/
	int spf_decode_get_int(spf_t* spf, char* key, int* value);

	/**
	 *@brief get float value from spf
	 *
	 * @param spf the instance of spf structure
	 * @param key the key of the float value you want to get
	 * @param value the float value you want to get
	 * @return int if success return 0,else return -1
	 */
	int spf_decode_get_float(spf_t* spf, char* key, float* value);


	/**
	 *@brief get string value from spf
	 *
	 * @param spf the instance of spf structure
	 * @param key the key of the string value you want to get
	 * @param value the string value you want to get
	 * @param len the buffer length of value buffer space you gave
	 * @return int if success return 0,else return -1 and if you string buffer is smaller than the real string length,return -2
	 */
	int spf_decode_get_string(spf_t* spf, char* key, char* value, int len);





#ifdef __cplusplus
}
#endif


#endif //__SPF_H__
